<?php
Class Manager_Model_category extends Zend_Db_Table
{	
	protected $_name ="category";
	protected $_primary ="id";
	
	public function list_category()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'category'));		          
        $results = $db->fetchAll($select);
        return $results;
	}
	public function sec_id()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'sections'),array('title','id'));		          
        $results = $db->fetchPairs($select);
        return $results;
	}
	
	public function listsec()
	{
		$db = Zend_Registry::get('connectDb');
        $select = $db->select()
		          ->from(array('ue' => 'sections'),array('id','title'));		          
        $results = $db->fetchPairs($select);
        return $results;
	}
	
	public function searchkq($keyword,$status=-1,$secnews=-1)
	{
		$db = Zend_Registry::get('connectDb');
			$query="SELECT c . *,s.title as theloai
                  	FROM category as c inner join sections as s on c.section_id = s.id 
					WHERE ((c.published=$status or $status=-1) AND (c.section_id =$secnews or $secnews=-1)AND (c.title like '%$keyword%'))						
					ORDER BY c.order desc";
			$result = $db->fetchAll($query)	;
		return $result;	
	}
	
   public function delete_muti($arrParam = null)
	{
    $db = Zend_Registry::get('connectDb');
	$tblGroup = new Manager_Model_category();
		if(count($arrParam['cid'])>0)
	 	{
			$ids = implode(',', $arrParam['cid']);
			$where = 'id IN (' . $ids . ')'; 
			$tblGroup->delete($where); 
		    
		    $where = "cat_id in($ids)";
		    $db->delete('article',$where);
	 	}
	 	
	 	
	}
	
	public function delete_data($id)
	{
		  	$tblGroup = new Manager_Model_category();
			$where = "id =$id";
		    $tblGroup->delete($where);  	
	}
	
	public function getrow($id)
	{
				$db = Zend_Registry::get('connectDb');
				$query="Select * from category  where id=$id";
				$result = $db->fetchAll($query);
				return $result;
	}
	public function edit($data)
	{
		$tblGroup = new Manager_Model_category();
		$where ='id ='.$data['id'];
    	$data1  = array('title'=>$data['title'],'published'=>$data['published'],
    	               'order'=>$data['order'],'sec_id'=>$data['sec_id']);
    	$tblGroup->update($data1,$where);
	}
	
   public  function listItemabout($status,$secnews)
	{
		 $db = Zend_Registry::get('connectDb');
			$query="SELECT c . *,s.title as theloai,l.title as ngonngu
                  	FROM   sections AS s INNER JOIN (category AS c INNER JOIN language AS l ON c.language_id=l.id )ON  c.section_id = s.id 
					WHERE ((c.published=$status or $status=-1)AND(c.section_id =$secnews or $secnews=-1 ))
					ORDER BY c.id desc";
			$result = $db->fetchAll($query)	;
			return $result;	
	}
	
   public function save($data)
	{	
      $introtext = stripslashes($data['introtext']);
	  
	  if ($data['key']=='add')
		  {	
			$db = Zend_Registry::get('connectDb');
		  	$data1 = array(
			              	'title'         	=> $data['title'],
		  	                'images'         	=> $data['images'],
		  	                'introtext'	        => $introtext,
		                	'section_id'	    => $data['section_id'],
		  	                'published'			=> $data['published'],
		  	                'featured'			=> $data['featured'],
							'order'	            => $data['order'],
	                        'language_id'	    => $data['language_id'],
		  			 );
	        $db->insert('category', $data1);
		  }
	else 
		  {
		  $tblGroup = new Manager_Model_category();
		  $where ='id ='.$data['id'];
    	 
		  $data1 = array(
			              	'title'         	=> $data['title'],
		  	                'images'         	=> $data['images'],
		  	                'introtext'	        => $introtext,
		                	'section_id'	    => $data['section_id'],
		  	                'published'			=> $data['published'],
		  	                'featured'			=> $data['featured'],
							'order'	            => $data['order'],
	                        'language_id'	    => $data['language_id'],
		  			 );
		  
    	  $tblGroup->update($data1,$where);
		  
		  }  
     }
     
	 public function delete_data_group($id)
	{
		  	$db = Zend_Registry::get('connectDb');
			$where = "categorys_id =$id";
		    $db->delete('user_privileges',$where);  	
	}
	
  public function delete_data_content($id)
	{
		  	$db = Zend_Registry::get('connectDb');
			$where = "cat_id =$id";
		    $db->delete('article',$where);  	
	}
	
}